﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.Utils;
using ActivitiesControl.Domain.Model.VOs;
using ActivitiesControl.Domain.Model.Entities;

namespace ActivitiesControl.Domain.Services.Impls
{
	[ExceptionMessageHandler]
	internal class ManageActivityService
		: BaseService, IManageActivityService
	{
		private ActivityHandler handler;

		public override event MessageHandler MessageRaised = delegate(object sender, MessageEventArgs e) { };

		internal override void RaiseMessage(object sender, MessageEventArgs e)
		{
			MessageRaised(sender, e);
		}

		public ManageActivityService()
		{
			handler = new ActivityHandler(ActivityRepository);
		}

		public Activity GetActivityById(int id)
		{
			return ActivityRepository.GetById(id);
		}

		public void SaveActivity(int? activityId, string description, string activityType, int effortHours, Date date, int projectId, int resourceId)
		{
			if (activityId.HasValue)
				UpdateActivity(activityId.Value, description, activityType, effortHours, date, projectId, resourceId);
			else
				RegisterActivity(description, activityType, effortHours, date, projectId, resourceId);
		}

		private void RegisterActivity(string description, string activityType, int effortHours, Date date, int projectId, int resourceId)
		{
			handler.Register(description, activityType, effortHours, date, projectId, resourceId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade registrada.", MessageType.Ok) });
		}

		private void UpdateActivity(int activityId, string description, string activityType, int effortHours, Date date, int projectId, int resourceId)
		{
			handler.Update(activityId, description, activityType, effortHours, date, projectId, resourceId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade atualizada.", MessageType.Ok) });
		}

		public void ExcludeActivity(int activityId)
		{
			handler.Exclude(activityId);
			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", "Atividade excluída.", MessageType.Ok) });
		}

		public IEnumerable<Activity> GetActivities(int resourceId, Date begin, Date end, int customerId, int projectId)
		{
			IList<Activity> list = new List<Activity>();

			var result = ActivityRepository.GetActivities(resourceId, begin, end, customerId, projectId);

			foreach (var activity in result)
			{
				activity.Project = ProjectRepository.GetById(activity.ProjectId);
				activity.Project.Customer = CustomerRepository.GetById(activity.Project.CustomerId);
				activity.Resource = ResourceRepository.GetById(activity.ResourceId);
				list.Add(activity);
			}

			MessageRaised(this, new MessageEventArgs { Message = new Message("Atividades", string.Format("{0} atividades retornadas.", list.Count), MessageType.Ok) });

			return list.OrderBy(a => a.ResourceId).OrderByDescending(a => a.Date.DateTime);
		}

		public IEnumerable<Customer> GetCustomersForForm(int resourceId)
		{
			return CustomerRepository.GetCustomersByResourceId(resourceId);
		}

		public IEnumerable<Project> GetProjectsForForm(int customerId)
		{
			return ProjectRepository.GetProjectsByCustomerId(customerId);
		}

		public IEnumerable<Resource> GetResourcesForForm(int resourceId)
		{
			return ResourceRepository.GetResourcesByResourceId(resourceId);
		}

		public IEnumerable<string> GetActivityTypesForForm(int customerId)
		{
			return ActivityRepository.GetActivityTypes(customerId);
		}

		public IEnumerable<Customer> GetCustomersForSearchFilter(int resourceId)
		{
			return CustomerRepository.GetCustomersByResourceId(resourceId);
		}

		public IEnumerable<Project> GetProjectsForSearchFilter(int customerId)
		{
			IEnumerable<Project> projects = ProjectRepository.GetProjectsByCustomerId(customerId);

			IList<Project> list = new List<Project>();

			list.Add(new Project { Name = "Todos", Id = -999 });

			foreach (var project in projects)
				list.Add(project);

			return list;
		}


	}
}